#include "tinyxml2.h"
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <fstream>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::ofstream;
using namespace tinyxml2;

struct RssItem{
    string title;
    string link;
    string description;
    string content;
};

class RssReader{
public:
    RssReader()
    {
        _rss.reserve(10);
    };
    void parseRss(const string & filename); //解析
    void dump(const string & filename); //输出
private:
    vector<RssItem> _rss;
};

void RssReader::parseRss(const string & filename){
    XMLDocument doc;
    doc.LoadFile(filename.c_str());

    if(doc.ErrorID()){
        cerr << "LoadFile error" << endl;
        return ;
    }

    XMLElement * itemNode = doc.FirstChildElement("rss")->FirstChildElement("channel")
        ->FirstChildElement("item");

    while(itemNode){
        string title  = itemNode->FirstChildElement("title")->GetText();
        string link  = itemNode->FirstChildElement("link")->GetText();
        string description  = itemNode->FirstChildElement("description")->GetText();
        string content  = itemNode->FirstChildElement("content:encoded")->GetText();

        std::regex htmlTagPattern("</?[^>]+>");

        string newDescription = std::regex_replace(description,htmlTagPattern,"");
        string newContent = std::regex_replace(content,htmlTagPattern,"");

        //将一个item内容加入_rss
        RssItem rss;
        rss.title = title;
        rss.link = link;
        rss.description = newDescription;
        rss.content = newContent;
        _rss.push_back(rss);

        //遍历
        itemNode = itemNode->NextSiblingElement("item");

    }

}

void RssReader::dump(const string & filename){
    //创建一个XML的文档对象
    XMLDocument* myDocument = new XMLDocument();

    for(size_t idx = 0; idx != _rss.size();++idx){
        //创建一个doc元素节点
        XMLElement *doc = myDocument->NewElement("doc");
        myDocument->LinkEndChild(doc);

        //创建一个docid元素并连接
        XMLElement * docid = myDocument->NewElement("docid"); 
        doc->LinkEndChild(docid);
        XMLText * docidText = myDocument->NewText(std::to_string(idx+1).c_str());
        docid->LinkEndChild(docidText);

        //创建一个title元素并连接
        XMLElement * title = myDocument->NewElement("title"); 
        doc->LinkEndChild(title);
        XMLText * titleText = myDocument->NewText(_rss[idx].title.c_str());
        title->LinkEndChild(titleText);

        //创建一个link元素并连接
        XMLElement * link = myDocument->NewElement("link"); 
        doc->LinkEndChild(link);
        XMLText * linkText = myDocument->NewText(_rss[idx].link.c_str());
        link->LinkEndChild(linkText);

        //创建一个description元素并连接
        XMLElement * description = myDocument->NewElement("description"); 
        doc->LinkEndChild(description);
        XMLText * descriptionText = myDocument->NewText(_rss[idx].description.c_str());
        description->LinkEndChild(descriptionText);

        //创建一个content元素并连接
        XMLElement * content = myDocument->NewElement("content"); 
        doc->LinkEndChild(content);
        XMLText * contentText = myDocument->NewText(_rss[idx].content.c_str());
        content->LinkEndChild(contentText);

    }
    //保存到文件
    myDocument->SaveFile("pagelib.txt");
    delete myDocument;
}

void test0(){
    RssReader rssReader;
    rssReader.parseRss("coolshell.xml");
    rssReader.dump("pagelib.txt");
}

int main()
{
    test0();
    return 0;
}

